layui.use(['jquery', 'common', 'table', 'form', 'layer', 'util'], function() {
	var $ = layui.jquery;
	var common = layui.common;
	var table = layui.table;
	var form = layui.form;
	var layer = layui.layer;
	var util = layui.util;

	table.render({
		elem: '#table',
		url: 'tableData',
		page: true,
		toolbar: '#tableToolbar',
		defaultToolbar: [],
		skin: 'line',
		request: {
			pageName: 'current',
			limitName: 'size'
		},
		cellMinWidth: 120,
		cols: [[
			{type: 'checkbox'},
			{field: 'servantGameId', title: '从者编号', templet: '<div>No.{{d.servant.gameId}}</div>', sort: true},
			{title: '从者头像', templet: common.tableImageTemplet('servant.avatar', 60, 'servant.name')},
			{title: '从者姓名', templet: '<div>{{d.servant.name}}</div>'},
			{field: 'getDate', title: '获得日期', sort: true, templet: common.tableDateTemplet('getDate', 'yyyy-MM-dd')},
			{field: 'getDays', title: '获得天数', sort: true},
			{field: 'stage', title: '再临阶段', sort: true},
			{field: 'holyGrail', title: '圣杯数', sort: true},
			{field: 'level', title: '等级', sort: true},
			{field: 'hp', title: 'hp', sort: true},
			{field: 'atk', title: 'atk', sort: true},
			{field: 'ffHp', title: 'hp强化', sort: true},
			{field: 'ffAtk', title: 'atk强化', sort: true},
			{field: 'bondLevel', title: '羁绊等级', sort: true, templet: function(d) {
				return d.bondValue >= 0 ? d.bondLevel : '';
			}},
			{field: 'bondValue', title: '羁绊值', sort: true},
			{field: 'dailyBond', title: '平均每日羁绊', minWidth: 135, sort: true},
			{field: 'skill1', title: '技能1等级', sort: true},
			{field: 'skill2', title: '技能2等级', sort: true},
			{field: 'skill3', title: '技能3等级', sort: true},
			{field: 'npLevel', title: '宝具等级', sort: true},
			{field: 'lastUpdateTime', title: '最后更新时间', sort: true, minWidth: 200, templet: common.tableDateTemplet('lastUpdateTime')},
			{title: '操作', minWidth: 250, toolbar: '#rowToolbar'}
		]],
		autoSort: false,
		initSort: {
			field: 'lastUpdateTime',
			type: 'desc'
		},
		where: {
			orderByField: 'lastUpdateTime',
			isAsc: false
		},
		done: function(res, curr, count) {
			common.tableImagePhotos(); 
		}
	});
	table.on('toolbar', function(obj) {
		switch (obj.event) {
		case 'add':
			common.toUrl('form');
			break;
		case 'delete':
			var data = table.checkStatus('table').data;
			if (data.length) {
				layer.confirm('确定要删除吗？', function(index) {
					var ids = [];
					$.each(data, function() {
						ids.push(this.id);
					});
					common.getJSON('delete', {ids: ids}, function(data) {
						layer.msg(data.msg);
						if (data.state == 'ok') {
							// 成功，刷新表格
							table.reload('table', {
								page: {
									curr: 1
								}
							});
						}
					});
					layer.close(index);
				});
			}
			break;
		default:
			break;
		}
	});
	table.on('tool', function(obj) {
		var data = obj.data;
		console.log(data);

		switch (obj.event) {
		case 'edit':
			common.toUrl('form?id=' + data.id);
			break;
		case 'del':
			layer.confirm('确定要删除吗？', function(index) {
				$.getJSON('delete', {ids: data.id}, function(data) {
					layer.msg(data.msg);
					if (data.state == 'ok') {
						// 成功，刷新表格
						table.reload('table', {
							page: {
								curr: 1
							}
						});
					}
				});
				layer.close(index);
			});
			break;
		case 'bond':
			common.toUrl('../myServantBond/list?id=' + data.id);
			break;
		default:
			break;
		}
	});
	table.on('sort', function(obj) {
		table.reload('table', {
			initSort: obj,
			where: {
				orderByField: obj.field,
				isAsc: obj.type ? obj.type == 'asc' : null
			}
		})
	});
	form.on('submit(search)', function(data) {
		table.reload('table', {
			where: data.field,
			page: {
				curr: 1
			}
		});
	});
});